package com.ruyuan.user.server.domain.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruyuan.common.model.PageDTO;
import com.ruyuan.user.api.model.enums.UserJobRelationTypeEnum;
import com.ruyuan.user.server.domain.converter.UserConverter;
import com.ruyuan.user.server.domain.model.User;
import com.ruyuan.user.server.domain.service.UserDomainService;
import com.ruyuan.user.server.domain.service.request.PageQueryUserParam;
import com.ruyuan.user.server.repository.bean.UserDO;
import com.ruyuan.user.server.repository.bean.UserExample;
import com.ruyuan.user.server.repository.bean.UserJobRelationDO;
import com.ruyuan.user.server.repository.bean.UserJobRelationExample;
import com.ruyuan.user.server.repository.mapper.UserJobRelationMapper;
import com.ruyuan.user.server.repository.mapper.UserMapper;
import com.ruyuan.user.server.util.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xx
 */
@Service
public class UserDomainServiceImpl implements UserDomainService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserConverter userConverter;

    @Autowired
    private UserJobRelationMapper userJobRelationMapper;

    @Override
    public Long saveUser(User user) {
        UserDO userDO = userConverter.convertModelToDO(user);
        if (user.getId() != null) {
            //更新
            userMapper.updateByPrimaryKeySelective(userDO);
        } else {
            //插入
            userMapper.insertSelective(userDO);
            user.setId(userDO.getId());
        }
        //保存用户岗位关系
        saveUserJob(user);
        return user.getId();
    }

    private void saveUserJob(User user) {
        UserJobRelationExample example = new UserJobRelationExample();
        example.createCriteria().andUserIdEqualTo(user.getId()).andRelationTypeEqualTo(UserJobRelationTypeEnum.MAIN_JOB.getCode());
        userJobRelationMapper.deleteByExample(example);
        UserJobRelationDO userJobRelationDO = userConverter.convertMainUserJobRelation(user);
        userJobRelationMapper.insert(userJobRelationDO);
    }

    @Override
    public User queryUserByUserName(String userName) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserNameEqualTo(userName).andIsDeletedEqualTo("n");
        List<UserDO> userDOS = userMapper.selectByExample(userExample);
        if (!CollectionUtils.isEmpty(userDOS)) {
            UserDO userDO = userDOS.get(0);
            return userConverter.convertDOToModel(userDO);
        }
        return null;
    }

    @Override
    public User queryUserById(Long userId) {
        if (userId == null) {
            return null;
        }
        UserDO userDO = userMapper.selectByPrimaryKey(userId);
        if (userDO == null) {
            return null;
        }
        return userConverter.convertDOToModel(userDO);
    }

    @Override
    public PageDTO<User> queryUserByPage(PageQueryUserParam param) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (StringUtils.isNotBlank(param.getName())) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            criteria.andPhoneLike("%" + param.getPhone() + "%");
        }
        if (StringUtils.isNotBlank(param.getCode())) {
            criteria.andFeaturesLike("%\"code\":\"" + param.getCode() + "\"%");
        }
        criteria.andIsDeletedEqualTo("n");
        PageHelper.startPage(param.getCurrent(), param.getPageSize());
        Page<UserDO> userDOS = (Page<UserDO>) userMapper.selectByExample(userExample);
        PageDTO<User> userPageDTO = new PageDTO<>();
        List<UserDO> result = userDOS.getResult();
        if (!CollectionUtils.isEmpty(result)) {
            userPageDTO.setData(result.stream().map(userConverter::convertDOToModel).collect(Collectors.toList()));
        }
        userPageDTO.setPages(userDOS.getPages());
        userPageDTO.setPageSize(userDOS.getPageSize());
        userPageDTO.setCurrent(param.getCurrent());
        userPageDTO.setTotal(userDOS.getTotal());
        userPageDTO.setCurPageSize(result.size());
        return userPageDTO;
    }

    @Override
    public boolean updatePsd(Long userId, String psd) {
        UserDO userDO = userMapper.selectByPrimaryKey(userId);
        if (userDO == null) {
            return false;
        }
        String newPsd = PasswordUtil.generatePassword(userDO.getUserName(), psd);
        userDO.setPassword(newPsd);
        userDO.setModifier(String.valueOf(userId));
        userDO.setGmtModified(new Date());
        userMapper.updateByPrimaryKeySelective(userDO);
        return true;
    }
}
